import sys

from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit

from registration.registered import RegisteredWindow
from ui.login_window import Ui_MainWindow
from connect_database.user_tools import UserTool
from main_win.interface import MainWindow


# 登录页面
class LoginWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(LoginWindow, self).__init__()
        self.main = None
        self.setupUi(self)
        self.tools = None
        self.label_3.clear()
        self.label_4.clear()
        self.pushButton.clicked.connect(self.jump_main)
        self.pushButton_2.clicked.connect(self.jump_registered)
        self.registration = RegisteredWindow()
        self.lineEdit_2.setEchoMode(QLineEdit.Password)

        self.lineEdit_2.setClearButtonEnabled(True)
        self.lineEdit_2.returnPressed.connect(self.jump_main)

    def jump_main(self):
        # 初始化用户工具
        self.label_3.clear()
        self.label_4.clear()
        name = self.lineEdit.text()
        self.tools = UserTool()
        user_id, user_name, pass_word = self.tools.get_user_info(name)
        # 用户名存在
        if user_name:
            # 密码存在
            pwd = self.lineEdit_2.text()
            if pass_word == pwd:
                print("显示页面")
                self.main = MainWindow(user_name, user_id, pass_word)
                self.main.show()
                self.hide()
            else:
                self.label_4.setText("密码不正确")
        else:
            self.label_3.setText("用户名不存在")
            self.lineEdit.clear()
        self.lineEdit_2.clear()

    def jump_registered(self):
        self.registration.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    l = LoginWindow()
    l.show()
    sys.exit(app.exec_())  # 程序关闭时退出进程
